<?php

/***************************************************************\
* FILE:		sys/pages/edit_dirm.inc
* SECTION:	pages > admin set
* FUNCTION:	edit the settings of a directory
* USES:	
* INCLUDES:	
* PRODUCES:	
* LAUNCHES:	sys/inputcheck.inc
\***************************************************************/


/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	if (!has_access("MODERATE") and !has_access("ADMIN"))
		errorpage(5);
}

/**
 * appropriate page action interpretations
 */
function page_inc_execute_action($action) {
	global $new;
	switch($action) {
		case "add":
			check_access("ADMIN");
			$new = 1;
			break;
		default:
			add_alert("unknown action");
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE, $CONFIG, $new;
	$CONTENT = array();
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];
	
	$CONTENT['title'] = $new ? "Een directory toevoegen" : "Directory #{$THISDIR['dir_id']} aanpassen";
	
	if ($_POST['submitted']) {
	
		if ($_POST['delete']) {
			if (!$_POST['confirmed']) {
				$CONTENT['form'] = form_confirm("Bent u zeker dat deze directory weg moet?");
				$PAGE['content'] = $CONTENT;
				return;
			} else if ($_POST['confirmed'] == "YES") {
				$sql = db_query_delete("DIRECTORY_TREE", "parent_id = {$THISDIR['parent_id']} AND child_id = {$THISDIR['dir_id']}");
				db_do_query($sql);
				redirect("/home");
			}
			$_POST['delete'] = 0;
			
		} else {
		
			include_once('sys/inputcheck.inc');
			
			// input checks
			$ok = 1;
			if ($new) {
				$ok = (check_tags($_POST['name'], "naam") and $ok) ? 1 : 0;
				$ok = (check_exists($_POST['name'], "naam") and $ok) ? 1 : 0;
			}
			$ok = (check_tags($_POST['menuname'], "menunaam") and $ok) ? 1 : 0;
			$ok = (check_exists($_POST['menuname'], "menunaam") and $ok) ? 1 : 0;
			$ok = (check_tags($_POST['title'], "titel") and $ok) ? 1 : 0;
			$ok = (check_exists($_POST['title'], "titel") and $ok) ? 1 : 0;
			$ok = (check_tags($_POST['info'], "info") and $ok) ? 1 : 0;
			$ok = (check_exists($_POST['info'], "info") and $ok) ? 1 : 0;
			if ($_POST['show_body']) {
				$ok = (check_tags_advanced($_POST['body_main'], "body") and $ok) ? 1 : 0;
				$ok = (check_tags_tables($_POST['body_main'], "body") and $ok) ? 1 : 0;
			}
			if ($_POST['body_footer']) {
				$ok = (check_tags_advanced($_POST['body_footer'], "footer") and $ok) ? 1 : 0;
				$ok = (check_tags_tables($_POST['body_footer'], "footer") and $ok) ? 1 : 0;
			}
			if ($new or ($_POST['relocate'] == "move") or ($_POST['relocate'] == "copy"))
				if ($_POST['location'] == "xxx") {
					$ok = 0;
					add_alert("Geen locatie aangeduid");
				}
				
			if ($ok) {
			
				list($parent_id, $rank) = explode("#", $_POST['location']);
				
				if ($new) {
				
					// insert directory
					$body = "NULL";
					if ($_POST['body_main']) {
						$sql = db_query_insert(array('body'), array(db_sqlize($_POST['body_main'])), "BODY");
						db_do_query($sql);
						$body = db_insert_id();
					}
					
					$footer = "NULL";
					if ($_POST['body_footer']) {
						$sql = db_query_insert(array('body'), array(db_sqlize($_POST['body_footer'])), "BODY");
						db_do_query($sql);
						$footer = db_insert_id();
					}
					
					$sql = db_query_insert(array('name', 'menuname', 'title', 'info', 'body_main', 'body_footer', 'sorting_id', 'show_body', 'show_subsets', 'show_items', 'show_subfora', 'allow_new_subs', 'crop_topiclist'), array(strtolower(db_sqlize($_POST['name'])), db_sqlize($_POST['menuname']), db_sqlize($_POST['title']), db_sqlize($_POST['info']), $body, $footer, $_POST['sorting_id'], $_POST['show_body'] ? 1 : 0, $_POST['show_subsets'] ? 1 : 0, $_POST['show_items'] ? 1 : 0, $_POST['show_subfora'] ? 1 : 0, $_POST['allow_new_subs'] ? 1 : 0, $_POST['crop_topiclist'] ? 1 : 0), "DIRECTORY");
					db_do_query($sql);
					$dir_id = db_insert_id();
					
					// insert tree listing
					$sql = db_query_update(array('rank'), array('rank+1'), "DIRECTORY_TREE", "parent_id = $parent_id AND rank >= $rank");
					db_do_query($sql);
					$sql = db_query_insert(array('parent_id', 'child_id', 'rank'), array($parent_id, $dir_id, $rank), "DIRECTORY_TREE");
					db_do_query($sql);
					
				} else {
					
					// update directory
					if ($PAGE['body_main_id'] > 0) {
						$body = $PAGE['body_main_id'];
						$sql = db_query_update(array('body'), array(db_sqlize($_POST['body_main'])), "BODY", "body_id = $body");
						db_do_query($sql);
					} else if ($_POST['body_main']) {
						$sql = db_query_insert(array('body'), array(db_sqlize($_POST['body_main'])), "BODY");
						db_do_query($sql);
						$body = db_insert_id();
					} else
						$body = "NULL";
						
					if ($PAGE['body_footer_id'] > 0) {
						$footer = $PAGE['body_footer_id'];
						$sql = db_query_update(array('body'), array(db_sqlize($_POST['body_footer'])), "BODY", "body_id = $footer");
						db_do_query($sql);
					} else if ($_POST['body_footer']) {
						$sql = db_query_insert(array('body'), array(db_sqlize($_POST['body_footer'])), "BODY");
						db_do_query($sql);
						$footer = db_insert_id();
					} else
						$footer = "NULL";
						
					$sql = db_query_update(array('menuname', 'title', 'info', 'body_main', 'body_footer', 'sorting_id', 'show_body', 'show_subsets', 'show_items', 'show_subfora', 'allow_new_subs', 'crop_topiclist'), array(db_sqlize($_POST['menuname']), db_sqlize($_POST['title']), db_sqlize($_POST['info']), $body, $footer, $_POST['sorting_id'], $_POST['show_body'] ? 1 : 0, $_POST['show_subsets'] ? 1 : 0, $_POST['show_items'] ? 1 : 0, $_POST['show_subfora'] ? 1 : 0, $_POST['allow_new_subs'] ? 1 : 0, $_POST['crop_topiclist'] ? 1 : 0), "DIRECTORY", "dir_id = {$_POST['dir_id']}");
					db_do_query($sql);
					
					// update/insert tree listing
					switch($_POST['relocate']) {
						case "none":
							break;
						case "move":
							$sql = db_query_update(array('rank'), array('rank+1'), "DIRECTORY_TREE", "parent_id = $parent_id AND rank >= $rank");
							db_do_query($sql);
							$sql = db_query_update(array('parent_id', 'rank'), array($parent_id, $rank), "DIRECTORY_TREE", "parent_id = {$THISDIR['parent_id']} AND child_id = {$_POST['dir_id']}");
							db_do_query($sql);
							break;
						case "copy":
							$sql = db_query_update(array('rank'), array('rank+1'), "DIRECTORY_TREE", "parent_id = $parent_id AND rank >= $rank");
							db_do_query($sql);
							$sql = db_query_insert(array('parent_id', 'child_id', 'rank'), array($parent_id, $_POST['dir_id'], $rank), "DIRECTORY_TREE");
							db_do_query($sql);
							break;
					}
				
				}
				redirect($PAGE['dirpath']);
			}
	
		}
	}

	$fields[] = form_input_hidden("dir_id", $new ? "NEW" : $THISDIR['dir_id']);
	
	if ($new)
		$fields[] = form_input_field("Naam", "name", "");
	
	$fields[] = form_input_field("Naam in menu", "menuname", $new ? "" : db_desqlize($THISDIR['menuname']), "", 45);
	$fields[] = form_input_field("Titel", "title", $new ? "" : db_desqlize($THISDIR['title']), "", 70);
	$fields[] = form_input_field("Info", "info", $new ? "" : db_desqlize($THISDIR['info']), "", 245);
	
	$sql = db_query_select(array(array('*')), array("SORTING"), 1);
	$sqlresult = db_do_query($sql);
	$keys = array();
	$vals = array();
	while ($r = db_fetch_array($sqlresult)) {
		array_push($keys, $r['sorting_id']);
		array_push($vals, $r['sorting_name']);
	}
	$fields[] = form_input_select("Sortering", "sorting_id", $keys, $vals, "kies een sortering", $new ? 1 : $THISDIR['sorting_id']);

	$fields[] = form_input_checkbox("Body tonen", "show_body", $new ? 1 : $THISDIR['show_body']);
	$fields[] = form_input_checkbox("Subsets tonen", "show_subsets", $new ? 1 : $THISDIR['show_subsets']);
	$fields[] = form_input_checkbox("Items tonen", "show_items", $new ? 1 : $THISDIR['show_items']);
	$fields[] = form_input_checkbox("Subfora tonen", "show_subfora", $new ? 1 : $THISDIR['show_subfora']);
	$fields[] = form_input_checkbox("Gebruikers mogen eigen subsets toevoegen", "allow_new_subs", $new ? 1 : $THISDIR['allow_new_subs']);
	$fields[] = form_input_checkbox("Topiclijst limiteren tot {$CONFIG['numtopics']} topics/subset", "crop_topiclist", $new ? 1 : $THISDIR['crop_topiclist']);
	
	$fields[] = form_input_text("Body", "body_main", $new ? "" : db_desqlize($PAGE['body_main']));
	$fields[] = form_input_text("Footer", "body_footer", $new ? "" : db_desqlize($PAGE['body_footer']));
	
	// menu (re)location only for admins
	if (has_access("ADMIN")) {
		
		if (!$new)
			$fields[] = form_input_select("Actie", "relocate", array("none", "move", "copy"), array("none", "move", "copy"), "", "none");
		
		$keys = array();
		$vals = array();
		
		list($keys, $vals) = generate_list(0, 0, $keys, $vals, $PAGE['menu']);
			
		$fields[] = form_input_select("Locatie", "location", $keys, $vals);
		
		if (!$new)
			$fields[] = form_input_checkbox("Verwijder deze directory", "delete");
		
	}
	
	$CONTENT['form'] = form_create("dirform", array_reverse($fields));
	
	$PAGE['content'] = $CONTENT;
}

function generate_list($parent, $level, $keys, $vals, $menu) {
		
	$rank = 0;	
	$space = "";
	for ($i = 0; $i < $level; $i++)
		$space .= "&nbsp;&nbsp;&nbsp;&nbsp;";
	while ($id = @array_shift($menu[$parent]['children'])) {
		$rank = $menu[$id]['rank'];
		array_push($keys, "$parent#$rank");
		array_push($vals, $space . "--hier--");
		array_push($keys, "xxx");
		array_push($vals, $space . $menu[$id]['menuname']);
		list($keys, $vals) = generate_list($id, ($level+1), $keys, $vals, $menu);
	}
	
	array_push($keys, "$parent#" . ($rank+1));
	array_push($vals, "$space--hier--");
	
	return array($keys, $vals);
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	print_form($PAGE['content']['form']);
}

?>
